<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ObjectToArray
 *
 * @author fabio
 */
class ObjectToArray {


    public static function getArray($object) {

        $arrayEl = array();

        if(is_array($object)) {
            foreach ($object as $el) {

                array_push($arrayEl, ObjectToArray::getArray($el));

            }

        } else {


            $reflect = new ReflectionClass($object);

            $props   = $reflect->getProperties(ReflectionProperty::IS_PRIVATE);

            // find all public and protected methods in ParentClass
            $parentProps = $reflect->getParentClass()->getProperties( ReflectionProperty::IS_PRIVATE );

            // find all parentmethods that were redeclared in ChildClass
            foreach($parentProps as $parentProp) {

                $value= "";

                $parentMethod = 'get' . ucfirst($parentProp->getName());

                 if (method_exists($object, $parentMethod)) {

                    $reflection_method = $reflect->getMethod($parentMethod);
                    $reflection_method->setAccessible(true);

                    $value = utf8_encode($reflection_method->invoke($object, $value));

                    if(is_numeric($value) && is_int($value))
                            $value = intval($value);
                        else if (is_numeric($value) && is_float($value))
                            $value = floatval($value);
                        else if (is_bool($value))
                            $value = boolval($value);

                    $arrayEl[$parentProp->getName()] = $value;

                }

            }


            foreach ($props as $prop) {

                $propertyName = $prop->getName();

                //Individuo il valore della property
                $methodName = 'get' . ucfirst($propertyName);

                if (method_exists($object, $methodName)) {
                    $value = null;

                    $reflection_method = $reflect->getMethod("$methodName");
                    $reflection_method->setAccessible(true);

                    $value = utf8_encode($reflection_method->invoke($object, $value));

                    if(is_numeric($value) && is_int($value))
                        $value = intval($value);
                    else if (is_numeric($value) && is_float($value))
                        $value = floatval($value);
                    else if (is_bool($value))
                        $value = boolval($value);

                    $arrayEl[$propertyName] = $value;

                }

            }

        }

        return $arrayEl;
    }

}
?>
